/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package simulador;

import java.awt.Graphics2D;
import java.awt.Point;
/*import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;*/
import java.util.ArrayList;
import java.util.Iterator;
/*import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;*/
import javax.swing.JLabel;

/**
 *
 * @author csantos
 */
public class Torre {

	int posX;
	int posY;
	public int dano;
	public ArrayList<Bot> listBots;
	private Bot tiroAtual;
	private Scenario scenario;
	public ArrayList<Bot> listTiros;
	private int tempo = 0;
	private JLabel image = new JLabel();

/*	public void setListBots(ArrayList<Bot> listBots) {
		this.listBots = listBots;
	}*/

	public void removeLabel()
	{
		scenario.remove(image);
	}

	public Torre(int x, int y, Scenario scenario) {
		posX = x;
		posY = y;
		this.scenario = scenario;
		listTiros = new ArrayList<Bot>();

		image.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/icoYoshi.png")));
		image.setBounds((int)posX-scenario.cellSpace/2, (int)posY-scenario.cellSpace/2, scenario.cellSpace, scenario.cellSpace);
		image.setVerticalAlignment(image.TOP);
		image.setHorizontalAlignment(image.LEFT);
		scenario.add(image);
	}

	public void atira(ArrayList<Bot> listBots, Graphics2D g2d){
		this.listBots = listBots;
		Bot b = calculaRetaTiro();
		
		tempo++;
		if(tempo%60==0 && b!=null)
		{
			tempo = 0;
			tiroAtual = new Bot(new Point(posX-15, posY-10), 10, 10, "bot", true, scenario,10, "/images/bola.gif");
			Point p = new Point((int) Math.round(b.xPosition + b.getRadius()), (int) Math.round(b.yPosition + b.getRadius()));
			tiroAtual.addGoal(new Goal(p, (int)b.getRadius()*2, (int)b.getRadius()*2, "goal"));
			tiroAtual.updateAtractive(1.1f);
			listTiros.add(tiroAtual);
		}
		drawTiros(g2d);

	}

	public ArrayList<Bot> getListTiros() {
		return listTiros;
	}

	private void drawTiros(Graphics2D g2d)
	{
		for(Iterator<Bot> it=listTiros.iterator(); it.hasNext();)
		{
			Bot tiro = it.next();
			tiro.updatePosition();
			if(tiro.xPosition>=scenario.getWidth() || tiro.xPosition<0 || tiro.yPosition<0 || tiro.yPosition>=scenario.getHeight())
			{
				tiro.removeLabel();
				it.remove();
			}
		}
	}

	private Bot calculaRetaTiro() {
		int i, j;
		double x, y;
		double m;

		int indiceMaiorPoder = 0;
		int maiorPoder = 0;

		for (i = 0; i < listBots.size(); i++) {
			Bot b1 = listBots.get(i);
			int numBotsAtingidos = 0;
			// calcula reta de tiro
			m = (b1.yPosition - posY) / (b1.xPosition - posX);
			for (j = 0; j < listBots.size(); j++) { // verifica quantos bots são atingidos
				Bot b2 = listBots.get(j);
				if (b1 != b2) {
					double yAUX = (b2.yPosition - posY) * m + posX;
					if (Math.abs(b1.xPosition - yAUX) < 5) {
						numBotsAtingidos++;
					}
				}
			}
			if (numBotsAtingidos > maiorPoder) {
				maiorPoder = numBotsAtingidos;
				indiceMaiorPoder = i;
			}
		}
		if(tiroAtual!=null && listBots.size() > 0 && tiroAtual.xPosition==listBots.get(indiceMaiorPoder).xPosition && tiroAtual.yPosition==listBots.get(indiceMaiorPoder).yPosition)
			return null;
		return (listBots.size()>0)?listBots.get(indiceMaiorPoder):null;
	}

/*	private void drawImage(Graphics2D g2d, String image, int x, int y) {
		BufferedImage img = null;
		try {
			img = ImageIO.read(new File(image));
		} catch (IOException ex) {
			Logger.getLogger(Scenario.class.getName()).log(Level.SEVERE, null, ex);
		}
		int w = img.getWidth(null);
		int h = img.getHeight(null);
		//BufferedImage bi = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
		AffineTransform origXform = g2d.getTransform();
		AffineTransform newXform = (AffineTransform) (origXform.clone());

//		newXform.rotate(Math.toRadians(rotate), xRot, yRot);
		g2d.setTransform(newXform);

		g2d.drawImage(img, x, y, null);
		g2d.setTransform(origXform); // Volta o eixo ao estado original
	}*/

/*	public void draw(Graphics2D g2d) {
		drawImage(g2d, "images/icoYoshi.png", posX - scenario.cellSpace / 2, posY - scenario.cellSpace / 2);//g2d.fillOval(posX - 50 / 2, posY - 50 / 2, 50, 50);
	}*/

	public void move(int x, int y) {
		posX = x;
		posY = y;
		image.setBounds((int)posX-scenario.cellSpace/2, (int)posY-scenario.cellSpace/2, scenario.cellSpace, scenario.cellSpace);
	}

	// Falta: Textura
}
